package jsyf.oa.service.app;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.app.FeedBackDao;
import jsyf.oa.entity.app.FeedBackEntity;
import jsyf.oa.facade.app.FeedBackFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xzq on 2018/1/24.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class FeedBackService implements FeedBackFacade {
    @Autowired
    private FeedBackDao feedBackDao;
    @Override
    public List<JSONObject> getFeedBackList(String jsonData) {
        StringBuilder sbCondition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        if (!StringUtils.isBlank(jsonParam.getString("createUserId"))) {
            sbCondition.append(" AND CREATE_USER_ID=? ");
            sqlArgs.add(jsonParam.getString("createUserId"));
        }
        if (!StringUtils.isBlank(jsonParam.getString("createUserName"))) {
            sbCondition.append(" AND (CREATE_USER_NAME LIKE '%?%' ");
            sqlArgs.add(jsonParam.getString("createUserName"));
        }
        if (!StringUtils.isBlank(jsonParam.getString("content"))) {
            sbCondition.append(" AND readerName like ?");
            sqlArgs.add("%" + jsonParam.getString("content") + "%");
        }
        return feedBackDao.getJSONListByCondition(sbCondition.toString(), sqlArgs.toArray());

}

    @Override
    public String save(String jsonData) {
        FeedBackEntity entity = JSON.parseObject(jsonData, FeedBackEntity.class);
        feedBackDao.add(entity);
        return entity.getId();
    }
}
